package com.msk.web.controller.wlhy;

import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.sys.service.SystemService;
import com.msk.wlhy.domain.HtDriverFile;
import com.msk.wlhy.domain.HtSupplier;
import com.msk.wlhy.domain.HtTransportDetail;
import com.msk.wlhy.service.HtCompanyService;
import com.msk.wlhy.service.HtDriverFileService;
import com.msk.wlhy.service.HtTransportDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/5/23
 * @description 小程序我的收入接口
 */
@Api(tags = "小程序我的收入接口")
@RestController
@RequestMapping("/api/waybillRevenueController")
public class WaybillRevenueController extends BaseController {

    @Autowired
    private HtDriverFileService htDriverFileService;
    @Autowired
    private HtTransportDetailService htTransportDetailService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private HtCompanyService htCompanyService;

    /**
     * @author llh
     * @date 2023/5/23
     * @return com.msk.common.core.domain.R<?>
     * @description 司机收入列表
     */
    @ApiOperation("司机收入列表")
    @PostMapping("/driverIncomList")
    public R<?> driverIncomList(@RequestBody Map<String, String> map) {
        HtDriverFile htDriverFile = TokenUtils.getDriverLogin();
        if (htDriverFile == null) {
            return R.appFail(400101, "用户未认证");
        }
        HtTransportDetail htTransportDetail = new HtTransportDetail();
        htTransportDetail.setDriverId(htDriverFile.getId());
        String status = map.get("status");
        if (StringUtils.isNotBlank(status)) {
            htTransportDetail.setStatus(status);
        }
        String dateStatus = String.valueOf(map.get("dateStatus"));
        String startDate = null;
        String endDate = null;
        if (dateStatus != null && "3".equals(dateStatus)) {
            startDate = String.valueOf(map.get("startDate"));
            endDate = String.valueOf(map.get("endDate"));
            endDate += " 23:59:59";
        }
        htTransportDetail.getParams().put("pageNum", map.get("pageNum"));
        htTransportDetail.getParams().put("pageSize", map.get("pageSize"));
        htTransportDetail.getParams().put("orderByStr", "a.create_date desc");
        startPageJson(htTransportDetail.getParams(), "");
        List<Map<String, Object>> list = htTransportDetailService.selectWaybillRevenueList(htTransportDetail, dateStatus, startDate, endDate);
        return R.appOk(getDataTable(list));
    }

    /**
     * @author llh
     * @date 2023/5/23
     * @return com.msk.common.core.domain.R<?>
     * @description 查看司机货主收入详情
     */
    @ApiOperation("查看司机货主收入详情")
    @PostMapping("/showIncomeDetail")
    public R<?> showIncomeDetail(@RequestBody HtTransportDetail htTransportDetail) {
        if (StringUtils.isEmpty(htTransportDetail.getId())) {
            return R.appFail(400101, "请求运单ID#htTransportDetailId为空");
        }
        HtDriverFile htDriverFile = TokenUtils.getDriverLogin();
        if (htDriverFile != null) {
            htTransportDetail.setDriverId(htDriverFile.getId());
        }
        HtTransportDetail result = htTransportDetailService.showDetailById(htTransportDetail);
        return R.appOk(result);
    }

    /**
     * 描述: 收入管理列表的收入统计
     * @author yangjy
     * @date 2023/6/20 17:51
     * @param
     * @return com.msk.common.core.domain.R<?>
     */
    @ApiOperation(value = "收入管理列表的收入统计")
    @PostMapping(value = "/ofCarrierRevenuesCount")
    public R<?> ofCarrierRevenuesCount(@RequestBody Map<String, Object> map) {
        HtSupplier login = TokenUtils.getSupplierLogin();
        if (login == null) {
            return R.appFail(400101, "该用户未进行身份认证");
        }
        String supplierUser = login.getId();
        String status = String.valueOf(map.get("status"));
        String startDate = null;
        String endDate = null;
        if (status != null && "3".equals(status)) {
            startDate = String.valueOf(map.get("startDate"));
            endDate = String.valueOf(map.get("endDate"));
            endDate += " 23:59:59";
        }
        List<Map<String, Object>> list = htTransportDetailService.ofCarrierRevenuesCount(supplierUser, status, startDate, endDate);
        return R.appOk(list);
    }

    /**
     * 描述: 承运商收入列表
     * @author yangjy
     * @date 2023/6/20 17:43
     * @param map
     * @return com.msk.common.core.domain.R<?>
     */
    @ApiOperation(value = "承运商收入列表")
    @PostMapping("/listOfCarrierRevenues")
    public R<?> listOfCarrierRevenues(@RequestBody Map<String, Object> map) {
        HtSupplier login = TokenUtils.getSupplierLogin();
        if (login == null) {
            return R.appFail(400101, "该用户未进行身份认证");
        }
        String supplierUser = login.getId();
        String status = String.valueOf(map.get("status"));
        String startDate = null;
        String endDate = null;
        if (status != null && "3".equals(status)) {
            startDate = String.valueOf(map.get("startDate"));
            endDate = String.valueOf(map.get("endDate"));
            endDate += " 23:59:59";
        }
        map.put("orderByStr", "td.create_date desc");
        startPageJson(map, "");
        List<Map<String, Object>> list = htTransportDetailService.findListOfCarrierRevenues(supplierUser, status, startDate, endDate);
        return R.appOk(getDataTable(list));
    }

    /**
     * @author llh
     * @date 2023/6/21
     * @return com.msk.common.core.domain.R<?>
     * @description 货主支出列表
     */
    @ApiOperation("货主支出列表")
    @PostMapping("/goodsMemberOutlayList")
    public R<?> goodsMemberOutlayList(@RequestBody HtTransportDetail htTransportDetail) {
        Map<String, Object> goodsMemberLogin = TokenUtils.getGoodsMemberLogin();
        if (goodsMemberLogin != null) {
            htTransportDetail.setContacts(String.valueOf(goodsMemberLogin.get("companyId")));
        } else {
            return R.appFail(400101, "用户未认证");
        }
        String dateStatus = htTransportDetail.getDateStatus();
        String startDate = null;
        String endDate = null;
        if (dateStatus != null && "3".equals(dateStatus)) {
            startDate = htTransportDetail.getFilterStartDate();
            endDate = htTransportDetail.getFilterEndDate();
            endDate += " 23:59:59";
        }
        htTransportDetail.getParams().put("orderByStr", "a.create_date desc");
        startPageJson(htTransportDetail.getParams(), "");
        List<Map<String, Object>> list = htTransportDetailService.selectWaybillRevenueList(htTransportDetail, dateStatus, startDate, endDate);
        return R.appOk(getDataTable(list));
    }
}
